import { StyleSheet } from 'react-native';
import { ThemeColors } from '../context/ThemeContext';

// 创建主题相关的通用样式
export const createThemedStyles = (theme: ThemeColors) => {
  return StyleSheet.create({
    // 容器样式
    container: {
      flex: 1,
      backgroundColor: theme.background,
    },
    
    safeContainer: {
      flex: 1,
      backgroundColor: theme.background,
      paddingHorizontal: 16,
    },
    
    // 卡片样式
    card: {
      backgroundColor: theme.card,
      borderRadius: 12,
      padding: 16,
      marginVertical: 8,
      shadowColor: theme.shadow,
      shadowOffset: { width: 0, height: 2 },
      shadowOpacity: 0.1,
      shadowRadius: 4,
      elevation: 3,
      borderWidth: 1,
      borderColor: theme.border,
    },
    
    elevatedCard: {
      backgroundColor: theme.cardElevated,
      borderRadius: 12,
      padding: 20,
      marginVertical: 8,
      shadowColor: theme.shadow,
      shadowOffset: { width: 0, height: 4 },
      shadowOpacity: 0.15,
      shadowRadius: 8,
      elevation: 6,
      borderWidth: 1,
      borderColor: theme.border,
    },
    
    // 文本样式
    title: {
      fontSize: 24,
      fontWeight: 'bold',
      color: theme.text,
      marginBottom: 8,
    },
    
    subtitle: {
      fontSize: 18,
      fontWeight: '600',
      color: theme.text,
      marginBottom: 6,
    },
    
    headline: {
      fontSize: 20,
      fontWeight: 'bold',
      color: theme.text,
      lineHeight: 28,
    },
    
    body: {
      fontSize: 16,
      color: theme.text,
      lineHeight: 24,
    },
    
    caption: {
      fontSize: 14,
      color: theme.textSecondary,
      lineHeight: 20,
    },
    
    label: {
      fontSize: 12,
      color: theme.textSecondary,
      fontWeight: '500',
    },
    
    // 按钮样式
    primaryButton: {
      backgroundColor: theme.buttonPrimary,
      paddingVertical: 12,
      paddingHorizontal: 24,
      borderRadius: 8,
      alignItems: 'center',
      justifyContent: 'center',
      minHeight: 48,
    },
    
    secondaryButton: {
      backgroundColor: theme.buttonSecondary,
      paddingVertical: 12,
      paddingHorizontal: 24,
      borderRadius: 8,
      alignItems: 'center',
      justifyContent: 'center',
      borderWidth: 1,
      borderColor: theme.border,
      minHeight: 48,
    },
    
    outlineButton: {
      backgroundColor: 'transparent',
      paddingVertical: 12,
      paddingHorizontal: 24,
      borderRadius: 8,
      alignItems: 'center',
      justifyContent: 'center',
      borderWidth: 2,
      borderColor: theme.primary,
      minHeight: 48,
    },
    
    disabledButton: {
      backgroundColor: theme.buttonDisabled,
      paddingVertical: 12,
      paddingHorizontal: 24,
      borderRadius: 8,
      alignItems: 'center',
      justifyContent: 'center',
      minHeight: 48,
    },
    
    // 按钮文本样式
    primaryButtonText: {
      color: theme.buttonText,
      fontSize: 16,
      fontWeight: '600',
    },
    
    secondaryButtonText: {
      color: theme.text,
      fontSize: 16,
      fontWeight: '600',
    },
    
    outlineButtonText: {
      color: theme.primary,
      fontSize: 16,
      fontWeight: '600',
    },
    
    disabledButtonText: {
      color: theme.buttonTextDisabled,
      fontSize: 16,
      fontWeight: '600',
    },
    
    // 输入框样式
    input: {
      backgroundColor: theme.inputBackground,
      borderWidth: 1,
      borderColor: theme.inputBorder,
      borderRadius: 8,
      paddingHorizontal: 16,
      paddingVertical: 12,
      fontSize: 16,
      color: theme.text,
      minHeight: 48,
    },
    
    inputFocused: {
      borderColor: theme.primary,
      borderWidth: 2,
    },
    
    inputError: {
      borderColor: theme.error,
      borderWidth: 2,
    },
    
    // 分割线
    divider: {
      height: 1,
      backgroundColor: theme.divider,
      marginVertical: 16,
    },
    
    // 列表项
    listItem: {
      backgroundColor: theme.card,
      paddingVertical: 16,
      paddingHorizontal: 16,
      borderBottomWidth: 1,
      borderBottomColor: theme.border,
      flexDirection: 'row',
      alignItems: 'center',
    },
    
    // 状态指示器
    successIndicator: {
      backgroundColor: theme.success,
      borderRadius: 4,
      paddingHorizontal: 8,
      paddingVertical: 4,
    },
    
    warningIndicator: {
      backgroundColor: theme.warning,
      borderRadius: 4,
      paddingHorizontal: 8,
      paddingVertical: 4,
    },
    
    errorIndicator: {
      backgroundColor: theme.error,
      borderRadius: 4,
      paddingHorizontal: 8,
      paddingVertical: 4,
    },
    
    infoIndicator: {
      backgroundColor: theme.info,
      borderRadius: 4,
      paddingHorizontal: 8,
      paddingVertical: 4,
    },
    
    // 浮动操作按钮
    fab: {
      position: 'absolute',
      bottom: 16,
      right: 16,
      width: 56,
      height: 56,
      borderRadius: 28,
      backgroundColor: theme.primary,
      alignItems: 'center',
      justifyContent: 'center',
      shadowColor: theme.shadow,
      shadowOffset: { width: 0, height: 4 },
      shadowOpacity: 0.3,
      shadowRadius: 8,
      elevation: 8,
    },
    
    // 导航栏样式
    header: {
      backgroundColor: theme.surface,
      borderBottomWidth: 1,
      borderBottomColor: theme.border,
    },
    
    // Tab栏样式
    tabBar: {
      backgroundColor: theme.surface,
      borderTopWidth: 1,
      borderTopColor: theme.border,
    },
    
    // 模态框样式
    modal: {
      backgroundColor: theme.surface,
      borderTopLeftRadius: 20,
      borderTopRightRadius: 20,
      paddingTop: 20,
    },
    
    modalOverlay: {
      backgroundColor: theme.overlay,
    },
  });
};

// 深色主题特定的样式增强
export const getDarkThemeEnhancements = (theme: ThemeColors) => {
  return {
    // 增强的阴影效果
    enhancedShadow: {
      shadowColor: '#000000',
      shadowOffset: { width: 0, height: 4 },
      shadowOpacity: 0.3,
      shadowRadius: 8,
      elevation: 8,
    },
    
    // 发光效果（用于重要元素）
    glowEffect: {
      shadowColor: theme.primary,
      shadowOffset: { width: 0, height: 0 },
      shadowOpacity: 0.3,
      shadowRadius: 8,
      elevation: 8,
    },
    
    // 渐变背景
    gradientBackground: {
      backgroundColor: theme.background,
    },
  };
};

// 空的增强样式（用于浅色主题）
export const getEmptyEnhancements = () => {
  return {
    enhancedShadow: {},
    glowEffect: {},
    gradientBackground: {},
  };
};

// 辅助函数：获取对比色
export const getContrastColor = (backgroundColor: string): string => {
  // 简单的对比度计算
  const hex = backgroundColor.replace('#', '');
  const r = parseInt(hex.substr(0, 2), 16);
  const g = parseInt(hex.substr(2, 2), 16);
  const b = parseInt(hex.substr(4, 2), 16);
  const brightness = (r * 299 + g * 587 + b * 114) / 1000;
  return brightness > 128 ? '#000000' : '#ffffff';
};

// 辅助函数：添加透明度
export const addOpacity = (color: string, opacity: number): string => {
  const alpha = Math.round(opacity * 255).toString(16).padStart(2, '0');
  return color + alpha;
};